######################################################################
#Replication file for "The Impact of China's AIIB on the World Bank"
#Authors: Jing Qian, James Vreeland, Jianzhi Zhao
#Codes to replicate Figure A.11
######################################################################
rm(list = ls())

#To install the bpCausal package (ver 0.0.1)
# install.packages('devtools', repos = 'http://cran.us.r-project.org') # if not already installed
# devtools::install_github('liulch/bpCausal')

#Load bpCausal package
library(bpCausal)

#Other packages used
library(tidyverse)
library(RColorBrewer)

#Setwd
setwd("C:/Users/qianj/Dropbox (Princeton)/AIIB_WB_Replication")

#Load custom functions
#Note: Given the number of tests performed in the appendix, 
#several custom functions are created to implement estimation methods in a more efficient way.
#These functions merely create a wrap-up of existing functions, like bpCausal and gsynth,
#in order to avoid typing parameters repeatedly.

source("code/custom_functions.R")

###################################
#Estimation for Figure A.11
###################################
#----------------
#(1) Load data
#----------------
load("data/data_main.RData")

#----------------
#(2) Setup
#----------------
#Setup
df.use = df.main
D.use = "aiib_founder_2016"
covs.all = c("gdppc_log_lag",
             "population_log_lag",
             "election_either_lag",
             "fdi_gdp_lag",
             "debt_gni_lag",
             "oda_gni_lag",
             "polity2_lag",
             "unsc_lag",
             "IdealPointDistance_lag")
X.use = Z.use = A.use = covs.all
dv.list = c("hard_project_count_all",
            "hard_project_count_s1",
            "hard_project_count_s1_b",
            "soft_project_count_all",
            "soft_project_count_s1",
            "soft_project_count_s1_b")

#----------------
#(3) Estimation
#~15 minutes
#----------------
result.a11 = bpcausal.group(dv.list = dv.list,
                            df.use = df.use,
                            D.use = D.use,
                            X.use = X.use,
                            Z.use = Z.use,
                            A.use = A.use)

###################################
#Produce Figure A.11
###################################
#-------------
#(1) Prepare results
#-------------
#Summarize
result.coding.use = lapply(result.a11,
                           effSummary)

#----------------
#(2) Parameters
#----------------
n = 2
xlim = c(-2, 2)
cex = 2
xtext = 0.65
colors = brewer.pal(n = 3,
                    name = "Dark2")
gap = 1/(n+2)

#----------------
#(3) Plot
#----------------
#----------
#Setup
png(filename = "figure/Figure_A11.png",
    height = 800,
    width = 1200)

par(mar = c(5, 15, 1, 1))

#----------
#Empty plot
plot(1,
     type = "n",
     xlab = "Estimated Average Treatment Effect on the Treated",
     ylab = "",
     yaxt = "n",
     xaxt = "n",
     xlim = xlim,
     ylim = c(n + 0.5, 0.5),
     cex.lab = 2)
box()

#------------------
#Hard
#------------------
#Original
points(x = result.coding.use$hard_project_count_all$est.avg[1],
       y = 1 - gap,
       col = colors[1],
       pch = 19,
       cex = cex)

arrows(x0 = result.coding.use$hard_project_count_all$est.avg[2],
       x1 = result.coding.use$hard_project_count_all$est.avg[3],
       y0 = 1 - gap,
       y1 = 1 - gap,
       length = 0.05,
       angle = 90,
       code = 3,
       col = colors[1],
       cex = cex,
       lwd = cex)

#mjsector1
points(x = result.coding.use$hard_project_count_s1$est.avg[1],
       y = 1,
       col = colors[2],
       pch = 19,
       cex = cex)

arrows(x0 = result.coding.use$hard_project_count_s1$est.avg[2],
       x1 = result.coding.use$hard_project_count_s1$est.avg[3],
       y0 = 1,
       y1 = 1,
       length = 0.05,
       angle = 90,
       code = 3,
       col = colors[2],
       cex = cex,
       lwd = cex)

#mjsector1, exclude
points(x = result.coding.use$hard_project_count_s1_b$est.avg[1],
       y = 1 + gap,
       col = colors[3],
       pch = 19,
       cex = cex)

arrows(x0 = result.coding.use$hard_project_count_s1_b$est.avg[2],
       x1 = result.coding.use$hard_project_count_s1_b$est.avg[3],
       y0 = 1 + gap,
       y1 = 1 + gap,
       length = 0.05,
       angle = 90,
       code = 3,
       col = colors[3],
       cex = cex,
       lwd = cex)

#------------------
#Soft
#------------------
#Original
points(x = result.coding.use$soft_project_count_all$est.avg[1],
       y = 2-gap,
       col = colors[1],
       pch = 19,
       cex = cex)

arrows(x0 = result.coding.use$soft_project_count_all$est.avg[2],
       x1 = result.coding.use$soft_project_count_all$est.avg[3],
       y0 = 2-gap,
       y1 = 2-gap,
       length = 0.05,
       angle = 90,
       code = 3,
       col = colors[1],
       cex = cex,
       lwd = cex)


#mjsector1
points(x = result.coding.use$soft_project_count_s1$est.avg[1],
       y = 2,
       col = colors[2],
       pch = 19,
       cex = cex)

arrows(x0 = result.coding.use$soft_project_count_s1$est.avg[2],
       x1 = result.coding.use$soft_project_count_s1$est.avg[3],
       y0 = 2,
       y1 = 2,
       length = 0.05,
       angle = 90,
       code = 3,
       col = colors[2],
       cex = cex,
       lwd = cex)


#mjsector1, exclude
points(x = result.coding.use$soft_project_count_s1_b$est.avg[1],
       y = 2+gap,
       col = colors[3],
       pch = 19,
       cex = cex)

arrows(x0 = result.coding.use$soft_project_count_s1_b$est.avg[2],
       x1 = result.coding.use$soft_project_count_s1_b$est.avg[3],
       y0 = 2+gap,
       y1 = 2+gap,
       length = 0.05,
       angle = 90,
       code = 3,
       col = colors[3],
       cex = cex,
       lwd = cex)

#------------------
#Other elements
#------------------
#Abline
abline(v = 0,
       lty = "dashed",
       lwd = cex,
       col = "gray")
#Legend
legend("topright",
       legend = c("Original Coding",
                  "Largest major-sector",
                  "Largest major-sector (Exclude Agri. & ICT)"),
       cex = cex,
       col = colors,
       lty = c(1, 1),
       bty = "n",
       lwd = cex)

#Axis
axis(side = 1,
     at = seq(min(xlim),
              max(xlim),
              1),
     cex.axis = cex)

text(x = min(xlim) - xtext,
     y = (1:2) + 0.1,
     xpd = NA,
     cex = cex,
     labels = c("Infrastructure",
                "Non-Infrastructure"))
#----------
#Close
dev.off()